//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package events_subscribe

import (
	"fmt"

	"atomgit.com/openlinksaas/proto-gen-go.git/events/events_code"
	"atomgit.com/openlinksaas/proto-gen-go.git/events_api"
)

func processCodeEvent(event *events_api.Event, cfgItemList []*CfgItem) (bool, error) {
	var err error
	var msg string

	{
		addCommentEvent := &events_code.AddCommentEvent{}
		if event.EventData.MessageIs(addCommentEvent) {
			err = event.EventData.UnmarshalTo(addCommentEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 创建代码评论 %s (会话ID:%s)。", event.UserDisplayName, event.ProjectName, addCommentEvent.Content, addCommentEvent.ThreadId)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.CodeEvCfg.AddComment {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		updateCommentEvent := &events_code.UpdateCommentEvent{}
		if event.EventData.MessageIs(updateCommentEvent) {
			err = event.EventData.UnmarshalTo(updateCommentEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 更新代码评论 %s (原内容:%s,会话ID:%s)。", event.UserDisplayName, event.ProjectName, updateCommentEvent.NewContent, updateCommentEvent.OldContent, updateCommentEvent.ThreadId)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.CodeEvCfg.UpdateComment {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		removeCommentEvent := &events_code.RemoveCommentEvent{}
		if event.EventData.MessageIs(removeCommentEvent) {
			err = event.EventData.UnmarshalTo(removeCommentEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 删除代码评论 %s (会话ID:%s)。", event.UserDisplayName, event.ProjectName, removeCommentEvent.Content, removeCommentEvent.ThreadId)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.CodeEvCfg.RemoveComment {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	return false, nil
}
